﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;

namespace Medianamik.UI.Mediasend.Logic
{
    public class MediasendCampaign
    {
        public int ID { get; private set; }
        public string Name { get; private set; }
        public CampaignStatus Status { get; private set; }
        public DateTime CreatedOn { get; private set; }
        public DateTime? ClosedOn { get; private set; }

        public MediasendCampaign(int id, string name, CampaignStatus status, DateTime createdOn)
            :this(id, name, status, createdOn, null)
        {

        }
        public MediasendCampaign(int id, string name, CampaignStatus status, DateTime createdOn, DateTime? closedOn)
        {
            ID = id;
            Name = name;
            Status = status;
            CreatedOn = createdOn;
            ClosedOn = closedOn;
        }

        internal static MediasendCampaign MakeCampaign(Dictionary<string, object> record)
        {
            var status = ToMediasendStatus(record["status"].ToString());
            return new MediasendCampaign(int.Parse(record["id"].ToString()),
                                         record["name"].ToString(),
                                         status,
                                         DateTime.Parse(record["created_on"].ToString()),
                                         status == CampaignStatus.Closed ? (DateTime?)DateTime.Parse(record["closed_on"].ToString()) : null);
        }

        private static CampaignStatus ToMediasendStatus(string status)
        {
            return (CampaignStatus)Enum.Parse(typeof(CampaignStatus), status, true);
        }
    }

    public enum CampaignStatus
    {
        Ongoing,
        Closed,
        Deleted
    }
}
